using System;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.UI;
using Resources;
using mojoPortal.Business.WebHelpers;
using eStore.Business;

namespace eStore.UI
{

    public partial class eStoreModule : SiteModuleControl
    {
        protected ModuleTitleControlCustom TitleControl;
        protected Store store;
        protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        protected eStoreConfiguration config = new eStoreConfiguration();

        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Site != null && Site.DesignMode) { return; }

            Load += Page_Load;

            LoadSettings();
            productList1.Store = store;
            productList1.PageId = PageId;
            productList1.ModuleId = ModuleId;
            productList1.SiteRoot = SiteRoot;
            productList1.CurrencyCulture = currencyCulture;
            productList1.EnableRatings = config.EnableRatingsInProductList;
            productList1.EnableRatingComments = config.EnableRatingComments;
            productList1.Settings = Settings;
            productList1.Config = config;
            productList1.PageSize = config.PageSizeSetting;
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Site != null && Site.DesignMode) { return; }
            SetupCss();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            TitleControl.EditUrl = SiteRoot + "/eStore/AdminDashboard.aspx";
            TitleControl.Visible = !RenderInWebPartMode;
            if (ModuleConfiguration != null)
            {
                Title = ModuleConfiguration.ModuleTitle;
                Description = ModuleConfiguration.FeatureName;
            }

            if (store.Guid == Guid.Empty)
            {
                // store not created yet
                pnlStore.Visible = false;
                pnlStoreClosed.Visible = true;
                return;
            }

            if (store.IsClosed)
            {
                litStoredClosed.Text = store.ClosedMessage;
                pnlStoreClosed.Visible = true;
                pnlStore.Visible = false;
                return;
            }
        }

        private void PopulateLabels()
        {
            lnkCart.PageID = PageId;
            lnkCart.ModuleID = ModuleId;
            //lnkCart.Text = "Cart";
            //lnkCart.NavigateUrl = Page.ResolveUrl(
            //    "~/eStore/Cart.aspx?pageid=" + PageID.ToString()
            //    + "&mid=" + ModuleID.ToString());

            TitleControl.EditText = eStoreResources.StoreManagerLink;
            litStoredClosed.Text = eStoreResources.StoreClosedDefaultMessage;
            pnlCartLink.Visible = config.ShowCartLinkSetting;
        }

        private void LoadSettings()
        {
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            store = new Store(siteSettings.SiteGuid, ModuleId);
            config = new eStoreConfiguration(Settings);
            pnlContainer.CssClass += config.CustomCssClassSetting;
        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
                                  {
                                      ID = "styleestore",
                                      Text = @"<link href='"
                                             + SiteUtils.GetSkinBaseUrl()
                                             +
                                             @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
                                  };

            stylesheet.Controls.Add(cssLink);
        }
    }
}